/*!
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

// 引入js和css文件
function include(id, path, file){
	if (document.getElementById(id)==null){
        var files = typeof file == "string" ? [file] : file;
        for (var i = 0; i < files.length; i++){
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + path + name + "'";
            document.write("<" + tag + (i==0?" id="+id:"") + attr + link + "></" + tag + ">");
        }
	}
}
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

jQuery.validator.addMethod("isMobile", function(value, element, param) {  
		var mobile = $(param).val();
		var reg = /^1[358]\d{9}$/;
		if(mobile.length != 11){     
			 return false;   
			} 
        if(!reg.test(mobile)){
        	 return false;   
	        }
	return true;
}, "手机号码格式有误！");
jQuery.validator.addMethod("ifMoney", function(money, element, param) {  
	var reg = /^\d+(\.\d+)?$/;
    if(!reg.test(money)){
    	 return false;   
     }
	return true;
}, "金额格式有误！");
jQuery.validator.addMethod("ifLargeDate", function(value, element, param) {  
	var startDate = $(param).val();
	if(startDate && value){
		 var date1 = new Date(startDate.replace(/-/g, "/"));
		 var date2 = new Date(value.replace(/-/g, "/"));
		 return date2 >= date1;
	 }
	return true;
}, "结束日期必须大于开始日期！");
jQuery.validator.addMethod("ifEqual", function(value, element, param) {  
	var pw1 = $(param).val();
	return value === pw1;
}, "两次密码输入不一致！");
Array.prototype.indexOf = function (val) {  
    for (var i = 0; i < this.length; i++) {  
        if (this[i] == val) {  
            return i;  
        }  
    }  
    return -1;  
};  
Array.prototype.removevalue = function (val) {  
    var index = this.indexOf(val);  
    if (index > -1) {  
        this.splice(index, 1);  
    }  
};  
//日期选择控件
function openDatePicker(id){
	var opt = {
			theme: 'sense-ui', //皮肤样式
			display: 'bubble', //显示方式 
			mode: 'clickpick', //日期选择模式
			dateFormat: 'yy-mm-dd', // 日期格式
			setText: '确定', //确认按钮名称
			cancelText: '取消',//取消按钮名称
			dateOrder: 'yymd', //面板中日期排列格式
			dayText: '日', monthText: '月', yearText: '年', //面板中年月日文字
			startYear:1900, //开始年份
			endYear:2050 //结束年份
		}
	$('#'+id).mobiscroll().date(opt).mobiscroll('show');    
}

/* 显示遮罩层 */
function showOverlay() {
	var windowHeight = window.parent.document.body.scrollHeight;
 	var windowWidth = window.parent.document.body.scrollWidth;
	$("#overlay", parent.document).height(windowHeight);
	$("#overlay", parent.document).width(windowWidth);

    // fadeTo第一个参数为速度，第二个为透明度
    // 多重方式控制透明度，保证兼容性，但也带来修改麻烦的问题
	$("#overlay", parent.document).fadeTo(200, 0.5);
	var id = "#messageBox";
	var w = $(id, parent.document).width();
 	var h = $(id, parent.document).height();
  	var t = (windowHeight/2) - (h/2);
    if(t < 0) t = 0;
   	var l = (windowWidth/2) - (w/2);
  	if(l < 0) l = 0;
    $(id, parent.document).css({left: l+'px', top: t+'px',display : 'block'});
}
 
 /* 隐藏覆盖层 */
 function hideOverlay() {
	 $("#overlay", parent.document).fadeOut(200);
	 $("#messageBox", parent.document).css('display','none');
 }
 
 function cloneJson (jsonObj) 
 {
     var buf;
     if (jsonObj instanceof Array) {
         buf = [];
         var i = jsonObj.length;
         while (i--) {
             buf[i] = arguments.callee(jsonObj[i]);
         }
         return buf;
     }else if (typeof jsonObj == "function"){
         return jsonObj;
     }else if (jsonObj instanceof Object){
         buf = {};
         for (var k in jsonObj) {
             buf[k] = arguments.callee(jsonObj[k]);
         }
         return buf;
     }else{
         return jsonObj;
     }
 }
function loadHelpPage(ctx, code, el){
	jQuery.ajax({  
        type : 'GET',  
        url : ctx + "/help/systemHelp/find?code=" + code,  
        success : function(data) {  
      	  var dataJson = eval("("+data+")");
      	  $(el).html(htmlDecode(dataJson.pageContext));
        }  
   });
}
//html编码
function htmlEncode(value) {
    if (value) {
        return jQuery('<div />').text(value).html();
    } else {
        return '';
    }
}
//html转码
function htmlDecode(value) {
    if (value) {
        return $('<div />').html(value).text();
    } else {
        return '';
    }
}